<?php

namespace App\Http\Requests\Admin;

use App\Http\Controllers\Controller;
use App\Models\Question;
use App\Models\Role;
use App\Models\User;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class QuestionRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     * @throws \Exception
     */
    public function authorize()
    {
        return auth()->user()->hasRole(Role::SUPER_ADMIN) || auth()->user()->hasAnyPermission(['questions']);
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'type' => ['required', Rule::in(Question::TYPE)],
            'category_id' => ['bail', 'required', 'exists:categories,id,category_group_id,4'],
            'fraction' => ['required', 'between:0,100', 'integer'],
            'level' => ['sometimes', 'required', 'between:0,5', 'integer'],
            'order' => ['sometimes', 'required', 'integer'],
            'analysis' => 'sometimes|required'
        ];
    }

    public function attributes()
    {
        return [
            'type' => '题目类型',
            'category_id' => '题目分类',
            'fraction' => '分数',
            'level' => '题目难度',
            'order' => '排序',
            'analysis' => '题目解析'
        ];
    }
}
